<template>
  <div class="wrapper_role">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row
        ><el-col
          ><el-button type="primary" @click="handelAddrolesClick"
            >添加角色</el-button
          ></el-col
        ></el-row
      >
      <el-table :data="RolesList" border stripe>
        <!-- 展开列 -->
        <el-table-column type="expand" width="50px">
          <template v-slot:default="slotProps">
            <el-row>
              <!-- 渲染一级权限 -->
              <el-col :span="6"></el-col>
              <!-- 渲染二级权限 -->
              <el-col :span="18"></el-col>
            </el-row>

            <pre>{{ slotProps.row }}</pre>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template v-slot:default="slotProps">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="handelEditRoleClick(slotProps.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handelDelteRoles(slotProps.row)"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="showSetRightsDialog(slotProps.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加用户角色 -->
    <el-dialog
      title="添加用户角色"
      :visible.sync="addRoledialogVisible"
      width="50%"
      top="100px"
    >
      <el-form
        :model="AddRoleForm"
        :rules="AddRoleFormRules"
        ref="AddRuleFormRef"
        label-width="100px"
        @close="handelAddRolecolseClick"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input
            type="text"
            v-model="AddRoleForm.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input
            type="text"
            v-model="AddRoleForm.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handelAddRolecolseClick">取 消</el-button>
        <el-button type="primary" @click="handelAddRoleConfrimClick"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 编辑用户角色 -->
    <el-dialog
      title="编辑用户"
      :visible.sync="editRoledialogVisible"
      width="50%"
      top="100px"
    >
      <el-form
        :model="EditRoleForm"
        :rules="EditRoleFormRules"
        ref="EditRoleFormRef"
        label-width="100px"
        @close="handelEditRolecolseClick"
      >
        <el-form-item label="角色ID" prop="id">
          <el-input
            type="text"
            v-model="EditRoleForm.id"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleName">
          <el-input
            type="text"
            v-model="EditRoleForm.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input
            type="text"
            v-model="EditRoleForm.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handelEditRolecolseClick">取 消</el-button>
        <el-button type="primary" @click="handelEditComfrimClick"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 分配权限 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRightsdialogVisible"
      width="50%"
    >
      <el-tree
        :data="treeData"
        :props="defaultProps"
        show-checkbox
        node-key="id"
        :default-expand-all="true"
        :default-checked-keys="defkeys"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightsdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRightsdialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  GETRolesList,
  AddRole,
  EditRoles,
  DeleteRoles,
  GETRightList
} from '../../request/api.js'
export default {
  name: 'Roles',

  data() {
    // 角色名称的校验
    const checkRoleName = (rules, val, callback) => {
      const checkRoleNameReg = /^[\u4e00-\u9fa5]{0,}$/
      if (checkRoleNameReg.test(val)) return callback()
      callback(new Error('请输入合法的名称（汉字）'))
    }
    return {
      RolesList: [],
      addRoledialogVisible: false,
      editRoledialogVisible: false,
      setRightsdialogVisible: false,
      AddRoleForm: {
        roleName: '',
        roleDesc: ''
      },
      AddRoleFormRules: {
        roleName: [
          { required: true, message: '请添加角色名称', trigger: 'blur' },
          { validator: checkRoleName, trigger: 'blur' }
        ]
      },
      EditRoleForm: {
        id: '',
        roleName: '',
        roleDesc: ''
      },
      EditRoleFormRules: {
        roleName: [
          { required: true, message: '请添加角色名称', trigger: 'blur' },
          { validator: checkRoleName, trigger: 'blur' }
        ]
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      // 默认选中的
      defkeys: []
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    //   获取所有角色的列表
    getRolesList() {
      GETRolesList()
        .then(res => {
          const { data, meta } = res.data
          if (!meta.status === 200) {
            return this.$message.error(meta.msg)
          }
          this.RolesList = data
          console.log(data)
        })
        .catch(error => this.$message(error))
    },

    //  点击添加用户角色
    handelAddrolesClick() {
      this.addRoledialogVisible = true
      if (this.$refs.AddRuleFormRef !== undefined) {
        this.$refs.AddRuleFormRef.resetFields()
      }
    },
    // 点击关闭 取消按钮
    handelAddRolecolseClick() {
      this.addRoledialogVisible = false
      this.$refs.AddRuleFormRef.resetFields()
    },
    // 点击确认的时候都
    handelAddRoleConfrimClick() {
      this.$refs.AddRuleFormRef.validate(valid => {
        if (!valid) return false
        AddRole(this.AddRoleForm)
          .then(res => {
            const { meta } = res.data
            if (!meta.status === 200) return this.$message.error(meta.msg)
            this.$message.success(meta.msg)
            this.getRolesList()
            this.addRoledialogVisible = false
          })
          .catch(error => {
            return this.$message.error(error)
          })
      })
    },
    // 编辑操作
    handelEditRoleClick(value) {
      this.editRoledialogVisible = true
      const { id, roleDesc, roleName } = value
      this.EditRoleForm = { id, roleDesc, roleName }
    },
    // 编辑对话框关闭按钮 和取消
    handelEditRolecolseClick() {
      this.editRoledialogVisible = false
      this.$refs.EditRoleFormRef.resetFields()
    },
    // 编辑对话框点击确定的时候
    handelEditComfrimClick() {
      const params = {
        roleName: this.EditRoleForm.roleName,
        roleDesc: this.EditRoleForm.roleDesc
      }
      this.$refs.EditRoleFormRef.validate(async valid => {
        if (!valid) return false
        EditRoles(this.EditRoleForm.id, params)
          .then(res => {
            const { meta } = res.data
            if (meta.status !== 200) return this.$message.error(meta.msg)
            this.editRoledialogVisible = false
            this.getRolesList()
            this.$message.success(meta.msg)
          })
          .catch(error => {
            this.$message.error(error)
          })
      })
    },
    // 删除角色
    handelDelteRoles(row) {
      DeleteRoles(row.id)
        .then(res => {
          const { meta } = res.data
          if (meta.status !== 200) {
            return this.$message.error(meta.msg)
          }
          this.$message.success(meta.msg)
          this.getRolesList()
        })
        .catch(error => this.$message.error(error))
    },
    showSetRightsDialog(data) {
      this.getLeafkeys(data, this.defkeys)
      console.log(this.defkeys)
      this.setRightsdialogVisible = true
      GETRightList('tree').then(res => {
        const { data, meta } = res.data
        if (meta.status !== 200) {
          return this.$message.success(meta.msg)
        }
        this.treeData = data
      })
    },
    getLeafkeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(element => {
        this.getLeafkeys(element, arr)
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
